/* 一个待办事项列表状态store */
// computed: 用于get方法. 派生状态. 比如订单金额 = 订单数量x单价. 订单金额就是computed
// 同样下面的 completedTodosCount 和 report 都是computed状态
// action: 用于set方法
//
/* 
{
  todos: [
    {
      task: '',
      completed: false,
      assignee: ''
    },
    ...
  ]
}
*/
import {observable, computed, action,  useStrict} from 'mobx'

useStrict(true)

class TodoStore {
  // 存储待办列表的数组
  @observable todos = []
  // 返回已完成待办事项的数目(get方法)
  @computed get completedTodosCount() {
    return this.todos.filter(
      todo => todo.completed === true
    ).length
  }
  // 报告当前待办事项列表的情况
  @computed get report() {
    if (this.todos.length === 0) {
      return '<无待办事项>'
    } else {
      return `进度: ${this.completedTodosCount} / ${this.todos.length}`
    }
  }
  // 添加一个待办事项(set方法)
  @action addTodo(task, assignee) {
    let todo = new Todo(task, false, assignee)
    this.todos.push(todo)
  }
}

// 待办事项项目结构
class Todo {
  @observable task = ''   
  @observable completed = false
  @observable assignee: ''
  @action setCompleted() {
    this.completed = !this.completed
  }
  @action setTodoTask(task) {
    this.task = task 
  }

  constructor(task, completed, assignee) {
    this.task = task
    this.completed = completed
    this.assignee = assignee
  }
}

const todoStore = new TodoStore()

export default todoStore
